import time

from selenium.webdriver import Chrome, ChromeService
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager

# service = ChromeService(executable_path=ChromeDriverManager().install())
opt = Options()
# 通过option参数，设置浏览器不关闭
opt.add_experimental_option('detach', True)
opt.add_argument('--start-maximized')  # 浏览器窗口最大化
web = Chrome(options=opt)
web.implicitly_wait(2)
web.get('http://lagou.com')

ele = web.find_element(By.XPATH, '//*[@id="changeCityBox"]/p[1]/a')
ele.click()
# 可能遇到的问题：此时点击之后，页面还没加载完成就开始执行后续代码，导致找不到指定的元素
# 解决方案：设置休眠等待，让浏览器缓一会儿再去执行后面的代码，https://www.selenium.dev/zh-cn/documentation/webdriver/waits/#implicit-waits
# time.sleep(30)
# 找到输入框，输入python  =》 输入回车/点击搜索按钮
web.find_element(By.XPATH, '//*[@id="search_input"]').send_keys('python', Keys.ENTER)

# 查找数据的位置，进行数据提取
# 找到页面中存放数据的所有的li
div_list = web.find_elements(By.XPATH, '//*[@id="jobList"]//div[@class="item__10RTO"]')
for div in div_list:
    # job_name = div.find_element(By.XPATH, '//a[@id="openWinPostion"]').text
    job_name = div.find_element(By.ID, 'openWinPostion').text
    # salary = div.find_element(By.XPATH, '//span[@class="money__3Lkgq"]').text
    salary = div.find_element(By.CLASS_NAME, 'money__3Lkgq').text
    print(f'{job_name},薪资：{salary}')
    # print(f'职位：{job_name}')
